﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ah : ControllerBase
{

    private bool keepTurning = true;
    private bool keepMoving = true;
    private float turnDir = 1;
    private Vector2 enemyPos;
    private Coroutine stf;

    private List<Coroutine> stopTurnCoroutine = new List<Coroutine>();
    private List<Coroutine> smallRangeTurningCoroutine = new List<Coroutine>();

    public override PlayerData GetPlayerData()
    {
        return new PlayerData()
        {
            Name = "啊？",
            Author = "ShiKi",
            BaseImage = "Sprite/Base/Base09",
            GunImage = "Sprite/Gun/Gun06"
        };
    }

    public override IEnumerator LoopAction()
    {
        while (true)
        {
            if (keepTurning)
            {
                //Debug.Log("ffffff");
                Turn(Time.realtimeSinceStartup * 100, 100);
            }
            if (keepMoving)
            {
                Move(1, Time.realtimeSinceStartup * 80, 0.1f);
            }
            yield return new WaitForSeconds(0.1f);
        }
    }

    public override void OnScanOthers(ScanOthersData scanOthersData)
    {
        if (scanOthersData.IsEnemy)
        {
            enemyPos = scanOthersData.othersPosition;
            //Debug.Log(GetAngle(scanOthersData.othersSpeed));
            StopTurn(); Stop();
            if(stf!=null)
                CoroutineMono.StopCoroutine(stf);
            stf = CoroutineMono.StartCoroutine(StopTurningFor(3.5f));
            //stopTurnCoroutine.Add(stf);
            Coroutine ssrt = CoroutineMono.StartCoroutine(StartSmallRangeTurning());
            smallRangeTurningCoroutine.Add(ssrt);
            //CoroutineMono.StartCoroutine(StopTurningFor(4f));
            //CoroutineMono.StartCoroutine(StartSmallRangeTurning(5f));
            CoroutineMono.StartCoroutine(StopMovingFor(0.2f));
            if (scanOthersData.othersSpeed != Vector2.zero)
                Move(1, GetAngle(scanOthersData.othersSpeed), 2f);
            //Debug.Log("GetAngle:"+GetAngle(scanOthersData.othersSpeed) + "   Speed:" + scanOthersData.othersSpeed);
            if (Vector2.Distance(Data.currentPosition, enemyPos) < 3 || Mathf.Abs(scanOthersData.othersAngle - Data.currentAngle) < 90)
            {

                Fire(1, 5);
            }
            else
            {
                if (scanOthersData.othersSpeed == Vector2.zero)
                {
                    Fire(1, 5);
                }
                else
                {
                    float angleToTurn = 18 / Vector2.Distance(Data.currentPosition, enemyPos);
                    //angleToTurn = 0;
                    //Debug.Log(angleToTurn);
                    Vector3 viewDir = Quaternion.Euler(0, 0, Data.currentAngle) * Vector3.right; viewDir = viewDir.normalized;
                    //Debug.Log(viewDir);

                    Vector3 normal = Vector3.Cross(viewDir, scanOthersData.othersSpeed);
                    //Debug.Log("normal:"+normal.normalized);
                    if (normal.z >= 0)
                    {
                        StopTurn();
                        Turn(Data.currentAngle + angleToTurn, 100);
                        //Fire(2, 5);
                        CoroutineMono.StartCoroutine(DelayShoot(0.05f, 1, 3));
                        //Debug.Log("当前角度：" + Data.currentAngle + "      " + "需要转的角度：" + (Data.currentAngle + angleToTurn));
                    }
                    else if (normal.z < 0)
                    {
                        Turn(Data.currentAngle - angleToTurn, -100);
                        //Fire(2, 5);
                        CoroutineMono.StartCoroutine(DelayShoot(0.05f, 1, 3));
                        //Debug.Log("当前角度：" + Data.currentAngle + "      " + "需要转的角度：" + (Data.currentAngle - angleToTurn));
                    }
                }
            }
        }
    }

    public override void OnScaned(float angle)
    {
        Stop();
        CoroutineMono.StartCoroutine(StopMovingFor(1f));

        Move(1, angle + 90, 2f);
    }

    public override void OnCrashWall()
    {
        Stop();
        CoroutineMono.StartCoroutine(StopMovingFor(3f));
        MoveTo(2, Vector3.zero);
    }

    public override void Start()
    {
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnCrashOthers(Vector3 othersPosition, float othersSpeed)
    {

    }

    public override void OnHitted(float bulletAngle)
    {
        float enemyPosAngle = bulletAngle + 180;
        if (enemyPosAngle > 360)
            enemyPosAngle -= 360;


        //if (stf != null)
        //    CoroutineMono.StopCoroutine(stf);
        //stf = CoroutineMono.StartCoroutine(StopTurningFor(1f));
        //foreach (Coroutine coroutine in smallRangeTurningCoroutine)
        //{
        //    CoroutineMono.StopCoroutine(coroutine);
        //}
        //smallRangeTurningCoroutine.Clear();
        //StopTurn();
        //float angleOffset = Data.currentAngle - enemyPosAngle;
        //if (angleOffset >= 0&& angleOffset <= 180)
        //{
        //    Turn(bulletAngle + 180, -100);
        //}
        //else if (angleOffset > 180)
        //{
        //    Turn(bulletAngle + 180, 100);
        //}
        //else if (angleOffset <= 0 && Mathf.Abs(angleOffset)<=180)
        //{
        //    Turn(bulletAngle + 180, 100);
        //}
        //else if (angleOffset <= 0 && Mathf.Abs(angleOffset) > 180)
        //{
        //    Turn(bulletAngle + 180, -100);
        //}
        //Turn(bulletAngle + 180, 100);


    }

    private IEnumerator StopTurningFor(float duration)
    {
        //foreach (Coroutine coroutine in stopTurnCoroutine)
        //{
        //    CoroutineMono.StopCoroutine(coroutine);
        //}
        //stopTurnCoroutine.Clear();
        keepTurning = false;
        yield return new WaitForSeconds(duration);
        keepTurning = true;
    }
    private IEnumerator StopMovingFor(float duration)
    {
        keepMoving = false;
        yield return new WaitForSeconds(duration);
        keepMoving = true;
    }
    private IEnumerator StartSmallRangeTurning()
    {
        foreach (Coroutine coroutine in smallRangeTurningCoroutine)
        {
            CoroutineMono.StopCoroutine(coroutine);
        }
        smallRangeTurningCoroutine.Clear();

        Turn(Data.currentAngle+40, 100);
        yield return new WaitForSeconds(0.6f);
        Turn(Data.currentAngle-80, -100);
        yield return new WaitForSeconds(1f);
        Turn(Data.currentAngle+80, 100);
        yield return new WaitForSeconds(1f);
        Turn(Data.currentAngle-80, -100);
        yield return new WaitForSeconds(1f);

    }


    private float GetAngle(Vector2 speed)
    {
        float angle = 0;
        if (speed.y <= 0)
        {
            angle = Vector2.Angle(Vector2.right, speed);
        }
        else
        {
            angle = 360 - Vector2.Angle(Vector2.right, speed);
        }
        return angle;
    }

    IEnumerator DelayShoot(float delay, int a, int b)
    {
        yield return new WaitForSeconds(delay);
        Fire(a, b);
    }

}
